package com.lzq.chatroom_project.common.utils;

import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;

public class SecurityUtil {

    public static String encrypt(String password) {
        String salt = UUID.randomUUID().toString().replace("-","");
        String finalPassword = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return salt + finalPassword;
    }

    public static Boolean verify(String password,String finalPassword) {
        if (!StringUtils.hasLength(password) || !StringUtils.hasLength(finalPassword)) {
            return false;
        }
        if (finalPassword.length() != 64) {
            return false;
        }
        String salt = finalPassword.substring(0,32);
        String finalPassword2 = DigestUtils.md5DigestAsHex((salt+password).getBytes());
        return (salt+finalPassword2).equals(finalPassword);
    }
}
